package org.dtrd.modules.archive.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState.ResponseState;
import org.dtrd.modules.archive.entity.bean.ReverseSchemeDetail;
import org.dtrd.modules.archive.entity.bean.UpdateSigninTemplate;
import org.dtrd.modules.archive.service.IDtrdEntRdArchiveReverseSchemeService;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.service.service.IDtrdEntRdServicePackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 逆转方案 前端控制器
 * </p>
 * @author QR
 * @since 2022-01-10
 */
@Api(tags = "逆转方案")
@RestController
@RequestMapping("/archive/reverse/scheme")
public class ReverseSchemeController extends BaseController {

    @Autowired
    private IDtrdEntRdArchiveReverseSchemeService reverseSchemeService;
    @Autowired
    private BaseCommonService baseCommonService;
    @Autowired
    private IDtrdEntRdServicePackageService packageService;

    @ApiOperation(value = "查询逆转方案详情", response = ReverseSchemeDetail.class)
    @GetMapping("get")
    public Result<?> getReverseScheme(@RequestParam Integer patientId){
        ReverseSchemeDetail info = reverseSchemeService.getReverseScheme(patientId);
        String logContent = String.format("查询逆转方案详情，查询患者id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return info != null ? Result.OK(info) : error(STATE_NO_RECORD);
    }

    @ApiOperation("新增或修改逆转方案")
    @PostMapping("edit")
    public Result<?> saveOrUpdateReverseScheme(@RequestBody @Validated ReverseSchemeDetail dto){
        String result = reverseSchemeService.saveReverseScheme(dto);
        String logContent = String.format("新增或修改逆转方案, 患者id: %d, 修改逆转方案内容%s,", dto.getPatientId(), dto);
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result==null ? Result.OK() : Result.error(STATE_OPERATION_FAILURE.getCode(), result);
    }

    @ApiOperation("删除逆转方案")
    @DeleteMapping("remove")
    public Result<?> removeReverseScheme(@RequestParam Integer patientId){
        if(isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = reverseSchemeService.removeReverseScheme(patientId);
        String logContent = String.format("删除逆转方案, 删除患者id: %d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("更改打卡计划")
    @PostMapping("update/signin")
    public Result<?> updateReverseSchemeSigninTemplate(@RequestBody @Validated UpdateSigninTemplate dto){
        boolean result = reverseSchemeService.updateReverseSchemeSigninTemplate(dto);
        String logContent = String.format("修改打卡模板, 患者id: %d, 模板信息%s,", dto.getPatientId(), dto);
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : Result.error("操作失败");
    }
}
